﻿using Architecture.CrossCutting.Common;
using Architecture.CrossCutting.Common.Dependency;
using Architecture.CrossCutting.Common.Logging;
using Architecture.CrossCutting.Web.WCF;
using Architecture.Domain.TestModel.Samples;
using Microsoft.Practices.Unity;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Architecture.CrossCutting.LoggingEntities.Logger;
using Architecture.CrossCutting.Web.Tracing.Model;

namespace Architecture.ServiceClient.Console
{

    public class Logger<WcfLogDetail> : IGenericLogger<WcfLogDetail> where WcfLogDetail : class
    {
        public void WriteError(string message, WcfLogDetail data, Exception ex)
        {
            System.Console.ForegroundColor = ConsoleColor.Red;
            System.Console.WriteLine(System.String.Format(@"Error : {0}", message));
            System.Console.WriteLine(System.String.Format(@"Log : {0}", data));
            System.Console.WriteLine(System.String.Format(@"Exception : {0}", ex));
            System.Console.ResetColor();
        }

        public void WriteInfo(string message, WcfLogDetail data)
        {
            //throw new Exception("C'est une erreur");
            System.Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine(System.String.Format(@"Info : {0}", message));
            System.Console.WriteLine(System.String.Format(@"Log : {0}", data));
            System.Console.ResetColor();
        }



    }


    class Program
    {
        static void Main(string[] args)
        {

            //System.Diagnostics.EventLog.CreateEventSource("PipelineDiagnosticClient", "Application");

            var container = new UnityContainer();
            //container.RegisterType<IGenericLogger<WcfLogDetail>, Logger<WcfLogDetail>>(new HierarchicalLifetimeManager());
            container.RegisterType<IGenericLogger<WcfClientLog>, WcfClientLogger>(new HierarchicalLifetimeManager());
            container.RegisterType<IGenericLogger<WebApiLog>, WebApiLogger>(new HierarchicalLifetimeManager());
            container.RegisterType<IExecutionContext, WinContext>();

            var result = container.Resolve<PipelineDiagnosticClient>();

            CustomContext.Current.DependencyResolver = new UnityResolver(container);


            //RunWcfAsync().Wait();

            Boolean continueTest = true;

            System.Console.CancelKeyPress += (object s, ConsoleCancelEventArgs cancelArgs) =>
            {
                continueTest = false;
                System.Console.WriteLine("Fin des tests");
                //System.Console.ReadLine();
            };

            Int32 i = 0;
            while (i < 100)
            {
                i++;
                //RunWcfAsync().Wait();
                RunAsync().Wait();

                if (!continueTest)
                {
                    break;
                }
                //System.Console.WriteLine("Encore: 2");
                //if (System.Console.ReadLine() == null)
                //{
                //    break;
                //}
            }
            System.Console.WriteLine("Fin des tests");
            System.Console.ReadLine();

        }


        static async Task RunWcfAsync()
        {
            System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
            watcher.Restart();
            WcfClient client = new WcfClient();
            List<Organisation> results = await client.GetOrganisations();
            watcher.Stop();
            System.Console.WriteLine("ThreadId {0} - Nb Result {1} - MeasureTime : {2} ms", System.Threading.Thread.CurrentThread.ManagedThreadId, results.Count, watcher.ElapsedMilliseconds);
        }

        static async Task RunAsync()
        {
            System.Diagnostics.Stopwatch watcher = new System.Diagnostics.Stopwatch();
            watcher.Restart();
            WebapiClient client = new WebapiClient();
            List<Organisation> results = await client.GetOrganisations();

            //Task<List<Organisation>>[] mytasks = new Task<List<Organisation>>[1];

            //for(Int32 i=0;i<1;i++)
            //{
            //    mytasks[i] = client.GetOrganisations();
            //}

            //Task.WaitAll(mytasks);

            watcher.Stop();
            System.Console.WriteLine("ThreadId {0} - Nb Result {1} - MeasureTime : {2} ms", System.Threading.Thread.CurrentThread.ManagedThreadId, results.Count, watcher.ElapsedMilliseconds);
        }
    }
}
